import {defineStore} from "pinia";
import {getCookies, model, msg, request, setCookies} from "@ut/tool";
import router from "@/router/index.js";

export const userStore = defineStore('userStore', {
    state: () => ({
        info: {},
        loading: false,
        list: [],
        total: 0,
    }),
    actions: {
        // 登录
        async login(data) {
            this.loading = true;
            let res = await request({url: '/user/login', method: 'post', data});
            if (res.data.state === 1) {
                msg('账号已锁定，无法登录', 'error');
            } else {
                this.info = res.data;
                msg('登录成功');
                setCookies('userInfo', this.info);
                router.replace({path: '/'});
            }
            this.loading = false;
        },

        // 获取用户信息
        getInfo() {
            return new Promise(async t => {
                let id = getCookies('userInfo').id;
                this.loading = true;
                let {data} = await request({url: '/user/getInfo', method: 'get', params: {id}});
                this.info = data;
                this.loading = false;
                t();
            })
        },

        // 查询
        get(params) {
            return new Promise(async t => {
                this.loading = true;
                let {data, total} = await request({url: '/user/get', method: 'get', params});
                this.list = data;
                this.total = total;
                this.loading = false;
                t();
            })
        },

        // 添加
        create(data) {
            return new Promise(async t => {
                this.loading = true;
                await request({url: '/user/create', method: 'post', data});
                msg('添加成功');
                t();
            })
        },

        // 修改
        update(data) {
            return new Promise(async t => {
                this.loading = true;
                await request({url: '/user/update', method: 'put', data});
                msg('修改成功');
                t();
            })
        },

        // 删除
        remove(id) {
            return new Promise(async t => {
                this.loading = true;
                let res = await model('提示', '正在删除用户，是否继续？', 'warning');
                if (res) {
                    await request({url: '/user/remove', method: 'delete', params: {id}});
                    msg('删除成功');
                    t();
                }
            })
        },

        // 修改密码
        updatePassword(data) {
            return new Promise(async t => {
                await request({url: '/user/updatePassword', method: 'put', data});
                msg('密码重置成功');
                t();
            })
        },

        // 重置密码
        resettingPassword(id) {
            return new Promise(async t => {
                let res = await model('提示', '正在重置密码，是否继续？', 'warning');
                if (res) {
                    await request({url: '/user/resettingPassword', method: 'put', data: {id}});
                    msg('密码重置成功');
                }
                t();
            })
        },

        // 导出
        reader(role) {
            return new Promise(async t => {
                let {data} = await request({url: '/user/reader', method: 'get', params: {role}});
                msg('导出成功');
                window.open(data);
                t();
            })
        },

        // 导入
        writer(url) {
            return new Promise(async t => {
                await request({url: '/user/writer', method: 'post', data: {url}});
                msg('导入成功');
                t();
            })
        },
    }
})